<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>平均值</title>
	</head>
	<body>
		<input type="button" value="生成一个数" id="btn1"/>
		<input type="button" value="平均值" id="btn2"/>
		<p id="num"></p>
		<p id="avg"></p>
		<script type="text/javascript">
			var a=[];
			var bt1=document.getElementById("btn1");
			var bt2=document.getElementById("btn2");
			bt1.onclick=function () {
				var p=document.getElementById("num");
				var r=parseInt(Math.random()*1000);
				a.push(r);
				var span=document.createElement("span");
				span.innerText=r;
				span.style.marginLeft="20px";
				p.appendChild(span);
			}
			bt2.onclick=function () {
				var s=0;
				for(var i in a){
					s=s+a[i];
				}
				document.getElementById("avg").innerText="平均值为："+(s/a.length);
			}
		</script>
	</body>
</html>
